$(function(){
	var uploading = false;
	$('.js_addimg').on('click', function() {//图片上传
		if (uploading) return;
		var $this = $(this);
		var $par = $this.parent().parent();
		var ImgFileGet = new ImgFileGet_class({
			fileSelector: "#image_upload_hide_input",  //fileInput选择器
			preViewImgSelector: "#pre-img",  //预览图片
			max_size: 2 * 1024,
			ajaxInterace: function (image) {
				var formData = new FormData();
				formData.append('default', image,'ken.jpg');
				uploaded = true;
				$.ajax({
					type: "post",
					url: "http://api.veryeast.cn/upload/feedback/default",
					data: formData,
					processData: false,
					contentType: false,
					success: function (data) {
						if (data.status == 1) {
							var imgSrc = data.data.path;
							var imgstr = '<img src="' + imgSrc + '" /><p class="delete_img">删除</p>';
							$par.parent().find('.has_img').addClass('has').append(imgstr);
							$this.siblings().attr('value', imgSrc);
						} else {
							alert('图片上传失败')
						}
					},
					error: function (err) {
						alert(JSON.stringify(err, null, 2))
					},
					complete: function () {
						$("#image_upload_hide_input").val("");
						uploading = false;
					}
				}, 'json')
			}
		})
	})

	//删除照片
	$('.has_img').on('click','.delete_img',function(){
		var $this = $(this);
		$("#image_upload_hide_input").val("");
		$this.parent().siblings().find('input').prop('value','');
		$this.parent().removeClass('has').empty();
	})
	
	$('.js_submit_btn').click(function(){ //提交报名
		
		var t = $('.js_register_for').serializeArray(); //序列化
  		var data = $('.js_register_for').serialize();
	
		if( changeName(t[0].value) ){ //姓名有误
			$('.js_name').addClass('err_border')
			$('.js_massage_name').css({'visibility':'inherit'})
			return false
		}else{
			$('.js_name').removeClass('err_border')
			$('.js_massage_name').css({'visibility':'hidden'})
		}
		
		if( !chagePhoneNumber(t[1].value) ){//手机号有误
			
			$('.js_mobile').addClass('err_border')
			$('.js_massage_mobile').css({'visibility':'inherit'})
			return false
		}else{
			$('.js_mobile').removeClass('err_border')
			$('.js_massage_mobile').css({'visibility':'hidden'})
		}
		
		if( changeName(t[2].value) ){ //任职酒店有误
			$('.js_hotel_name').addClass('err_border')
			$('.js_massage_hotel').css({'visibility':'inherit'})
			return false
		}else{
			$('.js_hotel_name').removeClass('err_border')
			$('.js_massage_hotel').css({'visibility':'hidden'})
		}
		
		if( changeName(t[3].value) ){ //职位
			$('.js_jobName').addClass('err_border')
			$('.js_massage_jobName').css({'visibility':'inherit'})
			return false
		}else{
			$('.js_jobName').removeClass('err_border')
			$('.js_massage_jobName').css({'visibility':'hidden'})
		}
		
		if( t[4].value == '' ){//工作照
			
			$('.js_err_img').html('请选择工作照').css({'visibility':'inherit'})
			return false
		}else{
			$('.js_err_img').css({'visibility':'hidden'})
		}
		
		if( t[5].value == '' ){ //变身照
			$('.js_err_img').html('请选择变身照').css({'visibility':'inherit'})
			return false
		}else{
			$('.js_err_img').css({'visibility':'hidden'})
		}
		
		
		$.ajax({
			type:"post",
			url:"/hotelp/api/sign-up",
			data:t,
			success:function(data){
				//console.log( data )
				var status = data.status;
				if( status ==  1 ){
					var id  = data.msg.id;
					window.location.href = 'http://activity.veryeast.cn/hotelp/index/share?id='+id;
				}else{
					//console.log( data.err_msg )
					for (var _key in data.err_msg) {
						//obj.hasOwnProperty('mobile');
					    //console.log( _key )
					    data.err_msg[_key] ? alert( data.err_msg[_key] ) : alert( '报名失败' );
					}
					
				}
			},
			error:function(){
				alert( '报名失败' )
			}
		});
		return false
	})
})


$('.pop_up .sure').click(function(){
	$('.background ,.pop_up').hide();
})


//验证手机号
function chagePhoneNumber(phone){
	var regPhone = /^1[3578]\d{9}$/;
	if( regPhone.test( phone ) ){
		return true
	}else{
		return false
	}
}

function changeName(name){
	var regName = /^(\d)*$/;
	if( regName.test(name) ){
		return true;

	}else{
		return false;
	}
}

//上传图片
var ImgFileGet_class = function(msg){

	this.fileSelector = msg.fileSelector;  //file-input的选择器
	this.preViewImgSelector = msg.preViewImgSelector;  //图片预览选择器
	this.max_size = msg.max_size || false;  //图片最大大小，不设为无限度
	this.ajaxInterace = msg.ajaxInterace; //ajax上传图片插件

	var _this = this;
	$(this.fileSelector).off('change').on('change',function(){
		var reader = new FileReader();
		var file = this.files[0];

		reader.onload = function(e){
			var com_rate = 3;

			if((_this.max_size!==false) && (file.size>_this.max_size)){
				com_rate =  _this.max_size/file.size;
			}

			_this.compressImg(e.target.result,com_rate,function(data){

				//_this.preViewImgSelector && $(_this.preViewImgSelector).attr('src',src_data);

				if(_this.ajaxInterace){
					_this.ajaxInterace(_this.dataURItoBlob(data));
				}
			});

		}
		reader.readAsDataURL(file);
	}).trigger('click');

	this.dataURItoBlob = function(base64Data) {
		var byteString;
		if (base64Data.split(',')[0].indexOf('base64') >= 0)
			byteString = atob(base64Data.split(',')[1]);
		else
			byteString = unescape(base64Data.split(',')[1]);
		var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];
		var ia = new Uint8Array(byteString.length);
		for (var i = 0; i < byteString.length; i++) {
			ia[i] = byteString.charCodeAt(i);
		}
		return new Blob([ia], {type:mimeString});
	};

	this.compressImg = function(imgData,com_rate,onCompress){
		if(!imgData)return;
		onCompress = onCompress || function(){};
		com_rate = com_rate || 1;//压缩比率默认为1
		var img = new Image();

		img.onload = function(){
			// 当图片宽度超过 400px 时, 就压缩成 400px, 高度按比例计算
			// 压缩质量可以根据实际情况调整
			var w = Math.min(600, img.width);
			var h = img.height * (w / img.width);
			var canvas = document.createElement('canvas');
			EXIF.getData(img, function() {
				var _orientation = EXIF.getTag(this, 'Orientation')
				//alert( _orientation );
				if (_orientation === 6) {
					alert(_orientation)
					canvas.width = h;
					canvas.height = w;
				} else {
					alert(_orientation)
					canvas.width = w;
					canvas.height = h;
				}
				var ctx = canvas.getContext('2d');
				if (_orientation === 6) {
					ctx.translate(canvas.width, 0);
					ctx.rotate(90 * Math.PI / 180)
				}
				// 把图片绘制到 canvas 中
				ctx.drawImage(img, 0, 0, w, h);
				// 取出 base64 格式数据
				onCompress(canvas.toDataURL("image/jpeg"))
			})
		};

		//记住必须先绑定事件，才能设置src属性，否则img没内容可以画到canvas
		img.src = imgData;
	};
}